package com.hui.server.controller;


import com.hui.server.entity.Department;
import com.hui.server.service.DepartmentService;
import com.hui.server.util.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.sql.SQLException;

/**
 * <p>
 *  前端控制器
 * </p>
 *             部门控制器，调用数据库存储过程
 * @author hui
 * @since 2022-03-31
 */
@RestController
@RequestMapping("/system/basic/department")
@Slf4j
public class DepartmentController {

    private DepartmentService departmentService;

    @Autowired
    public DepartmentController(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }


    @ApiOperation(value = "获取所有部门")
    @GetMapping("/")
    public Result<?> getAllDepartments(){
        return departmentService.getAllDepartments();
    }

    @ApiOperation(value = "添加部门")
    @PostMapping("/")
    public Result<?> addDep(@RequestBody Department department){
        try {
            return departmentService.addDep(department);
        } catch (SQLException throwables) {
            log.error("check：{}",throwables.getMessage());
            return Result.error(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()),"服务器异常，添加失败！");

        }
    }

    @ApiOperation(value = "删除部门")
    @DeleteMapping("/{id}")
    public Result<?> deleteDep(@PathVariable Integer id){
        try {
            return departmentService.deleteDep(id);
        } catch (SQLException throwables) {
            log.error("check：{}",throwables.getMessage());
            return Result.error(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()),"服务器异常，删除失败！");

        }
    }

    @ApiOperation("修改部门，只允许修改名字和是否启用")
    @PutMapping("/")
    public Result<?> updateDepById(@RequestBody Department department){
        return departmentService.updateDepById(department);
    }


}
